package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.vo.PageData;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.entity.Sku;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.entity.SpuDetail;
import cn.tedu.csmall.product.pojo.param.SkuAddNewParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.ISkuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * @author Administrator
 * @description 针对表【pms_sku(SKU（Stock Keeping Unit）)】的数据库操作Service实现
 * @createDate 2023-06-15 14:22:58
 */
@Service
@Slf4j
public class SkuServiceImpl implements ISkuService {

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private AttributeTemplateMapper attributeTemplateMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Override
    public void addNew(SkuAddNewParam skuAddNewParam) {
        //检查SPU ID是否存在
        //检查属性模版id是否存在
        //检查相册id是否存在
        log.info("开始处理[添加SKU]的业务");
        log.debug("参数:{}", skuAddNewParam);

        //检查相册ID是否存在
        Long albumID = skuAddNewParam.getAlbumId();
        AlbumStandardVO albumStandardVO = albumMapper.getStandardById(albumID);
        if (albumStandardVO == null) {
            log.info("根据相册ID查询，结果为null，抛出异常");
            String message = "新增Sku失败，相册数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //检查属性模版ID是否存在
        Long attributeTemplateID = skuAddNewParam.getAttributeTemplateId();
        AttributeTemplateStandardVO attributeTemplateStandardVO = attributeTemplateMapper.getStandardById(attributeTemplateID);
        if (attributeTemplateStandardVO == null) {
            log.info("根据属性模版ID查询，结果为null，抛出异常");
            String message = "新增Sku失败，属性模版数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }




        //检查类别ID是否存在
        Long categoryID = skuAddNewParam.getSpuId();
        SpuStandardVO spuStandardVO = spuMapper.getStandardById(categoryID);
        if (spuStandardVO == null) {
            log.info("根据SPUID查询，结果为null，抛出异常");
            String message = "新增Sku失败，SPU数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        long skuId = new Random().nextInt(1000000);
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuAddNewParam,sku);
        sku.setId(skuId)
                .setSales(0)
                .setCommentCount(0)
                .setPositiveCommentCount(0)
                .setGmtCreate(LocalDateTime.now())
                .setGmtModified(LocalDateTime.now());

        log.debug("新增Sku数据,spu={}",sku);
        int rows = skuMapper.insert(sku);
        if (rows !=1){
            String message = "添加SKU失败！服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("添加SKU成功！");



    }

    @Override
    public void deleteById(Long id) {
        log.info("开始处理【删除Sku】的业务");
        log.debug("传入参数,id={}", id);

        //检查SkuID检查Sku是否存在数据，如果不存在，则抛出异常
        SkuStandardVO standardById = skuMapper.getStandardById(id);
        log.info("根据Sku id检索数据库结果,standardById={}", standardById);
        if(standardById == null){
            String message = "删除Sku失败，Sku数据不存在！";
            log.info("根据Sku id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = skuMapper.deleteById(id);
        if(rows != 1){
            String message = "删除Sku失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.info("结果1:删除Sku结果,rows={}", rows);
    }

    @Override
    public SkuStandardVO getStandardById(Long id) {
        log.info("开始:【根据ID查询Sku】");
        log.debug("传入参数,id={}", id);
        SkuStandardVO standardById = skuMapper.getStandardById(id);

        //根据SkuID检查Sku是否存在数据，如果不存在，则抛出异常
        if(standardById==null){
            log.info("结果2:根据Sku id查询详情，结果为null，抛出异常...");
            String message = "查询Sku详情失败，Sku数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.info("结果1:查询出数据,standardById");
        return standardById;
    }

    @Override
    public PageData<SkuListItemVO> listBySpuId(Long spuId, Integer pageNum) {
        Integer pageSize = 5;
        return listBySpuId(spuId, pageNum, pageSize);
    }

    @Override
    public PageData<SkuListItemVO> listBySpuId(Long spuId, Integer pageNum, Integer pageSize) {
        log.info("开始:【查询-Sku-分页】");
        log.debug("传入参数, spuId={}, pageNum={}, pageSize={}", spuId, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<SkuListItemVO> list = skuMapper.listBySkuId(spuId);
        PageInfo<SkuListItemVO> pageInfo = new PageInfo<>(list);
        PageData<SkuListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("结果1: 查询结果：pageData={}", pageData);
        return pageData;
    }
}




